<template>
  <div class="container">
    <el-form class="demo-ruleForm">
      <el-form-item>
        <el-select v-model="search.type" placeholder="请选择业务类型">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-input
          style="width: 300px"
          :disabled="true"
          placeholder="请搜索商品信息"
          v-model="commodityNameList.name"
        >
          <template slot="prepend">商品</template>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="commodityBtn"
          ></el-button>
        </el-input>

        <el-input
          style="width: 300px"
          :disabled="true"
          placeholder="请搜索仓库"
          v-model="warehouseList.name"
        >
          <template slot="prepend">仓库</template>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="warehouseBtn"
          ></el-button>
        </el-input>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          class="handleSearch"
          @click="handleSearch()"
          >查询</el-button
        >
        <el-button
          size="small"
          type="danger"
          class="handleSearch"
          icon="el-icon-delete"
          @click="empty()"
          >清空</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column v-if="select.is_locks == 1" type="selection" align="center"></el-table-column> -->
      <el-table-column
        prop="goods_name"
        label="名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_name"
        label="仓库"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock"
        label="库存"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_num"
        label="实际库存"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="盘点前金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="after_price"
        label="差异后金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="typeMsg"
        label="类型"
        align="center"
      ></el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- dialog -->
    <checkDetailDialog
      :particularsDialogs="particularsDialogs"
    ></checkDetailDialog>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
  </div>
</template>
<script>
import CheckDetailDialog from "@/components/check/checkDetailDialog";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import Pagination from "@/components/pagination";
import { updateStockDetails } from "@/api/check/check";
import Warehouseselect from "@/components/commodity/warehouseselect";
export default {
  components: {
    pagination: Pagination,
    checkDetailDialog: CheckDetailDialog,
    commodityDialog: CommodityDialog,
    warehouseselect: Warehouseselect,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
      },
      commoditydialog: {
        centerDialogVisible: false,
      },
      commodityNameList: {
        name: "",
        code: "",
      },
      formData: [],
      formId: [],
      search: {
        type: "",
      },
      options: [
        {
          value: "1",
          label: "盘点入库 ",
        },
        {
          value: "2",
          label: "盘点出库",
        },
      ],
      particularsDialogs: {
        centerDialogVisible: false,
        id: "",
      },
      searchName: "",
      warehouseselect: {
        centerDialogVisible: false,
      },
      warehouseList: {
        name: "",
        code: "",
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        goods_id: this.commodityNameList.code,
        warehouse_code: this.warehouseList.code,
        type: this.search.type,
      };

      updateStockDetails(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.data.forEach((data) => {
              data.typeMsg = data.type == 1 ? "盘点入库" : "盘点出库";
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true,
      };
    },
    //选择库存
    selectVal(val) {
      this.select.is_locks = val;
      this.getData();
    },
    //盘点
    addPermission() {
      this.formId = [];
      if (this.formData.length == 0) {
        this.$message({
          message: "请选择商品",
          type: "info",
        });
      } else {
        this.dialog.centerDialogVisible = true;
        this.formData.forEach((res) => {
          this.formId.push({ goods_id: res.goods_id, spec: res.spec });
        });
      }
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //详情
    particulars(scope) {
      this.particularsDialogs.centerDialogVisible = true;
      this.particularsDialogs.id = scope.row.take_stock_goods_id;
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true,
      };
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //清空
    empty() {
      this.search = {
        type: "",
      };
      this.commodityNameList.code = "";
      this.commodityNameList.name = "";
      this.warehouseList.code = "";
      this.warehouseList.name = "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.formData = val;
      console.log(this.formData);
    },
  },
};
</script>
<style scoped>
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>